<!--
 * @Author: 张久冬
 * @Date: 2021-12-21 15:31:43
 * @
# Description:
-->
<template>
  <div class="login-account">
    <el-form label-width="60px" :rules="rules" :model="account" ref="fromRef">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { rules } from '../config/account-config'
import { ElForm } from 'element-plus'
import LocalCache from '../../../utils/cache'

export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      name: LocalCache.getCache('name') ?? 'coderwhy',
      password: LocalCache.getCache('password') ?? '123456'
    })
    const fromRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      // 验证
      fromRef.value?.validate((valid) => {
        if (valid) {
          // console.log('校验通过正在登陆')
          // 判断是否需要记住密码
          if (isKeepPassword) {
            LocalCache.setCache('name', account.name)
            LocalCache.setCache('password', account.password)
          } else {
            LocalCache.deleteCache('name')
            LocalCache.deleteCache('password')
          }
          // 进行登陆验证
          store.dispatch('login/accountLoginAction', { ...account })
        }
      })

      // 登陆
    }

    return {
      rules,
      fromRef,
      account,
      loginAction
    }
  }
})
</script>

<style scoped></style>
